<template>
  <div id="app">
    <el-input v-model="form.itemBrand" placeholder="请输入品牌名称"></el-input>
    <el-input v-model="form.itemName" placeholder="请输入商品名称"></el-input>
    <el-button type="primary" @click="select()">查询</el-button>

    <el-table :data="tableData" border style="width: 100%">
      <el-table-column fixed prop="id" label="id" width="200"></el-table-column>

      <el-table-column fixed prop="itemId" label="商品id" width="200"></el-table-column>
      <el-table-column prop="itemName" label="商品名称" width="200"></el-table-column>
      <el-table-column prop="itemImage" label="商品图片" width="200"></el-table-column>
      <el-table-column prop="itemDesc" label="商品描述" width="200"></el-table-column>
      <el-table-column prop="itemPrice" label="价格" width="300"></el-table-column>
      <el-table-column prop="itemBrand" label="品牌" width="300"></el-table-column>
      <el-table-column prop="itemColor" label="颜色" width="200"></el-table-column>
      <el-table-column prop="sum" label="数量" width="200"></el-table-column>
      <el-table-column label="数量">
        <template slot-scope="scope">
          <el-input v-model="scope.row.sum" placeholder="请输入数量"></el-input>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="100">
        <template slot-scope="scope">
          <el-button @click="updateCar(scope.row)" type="text" size="small">确定</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="pageNo"
      :page-sizes="[1, 2, 3, 4]"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    ></el-pagination>
  </div>
</template>

<script>
export default {
  mounted() {
    this.getRequest();
  },
  methods: {
    select() {
      console.log(this.value2);
      console.log(this.form);
      this.getRequest();
    },
    getRequest() {
      this.form.pageNo = this.pageNo;
      this.form.pageSize = this.pageSize;
      var $this = this;
      this.axios({
        method: "get",
        url: "http://localhost:9000/shoppingcar/myCarList",
        params: this.form
      }).then(function(res) {
          console.log(res);
        $this.tableData = res.data.obj.list;
        $this.total = res.data.obj.cout;
      });
    },
    updateCar(row) {
      var params = { itemId: row.id, sum: row.sum };
      console.log(row);
      var $this = this;
      this.axios({
        method: "post",
        url: "http://localhost:9000/shoppingcar/car/update",
        params: params
      }).then(function(res) {
        alert(res.data.msg);
        console.log("------------------>{}", res);
        if (res.data.code == "401") {
          //跳转到登录页面
          $this.$router.replace({ path: "/Login" });
        }
      });
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.pageSize = val;
      this.getRequest();
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.pageNo = val;
      this.getRequest();
    }
  },

  data() {
    return {
      tableData: [],
      pageNo: 1,
      pageSize: 2,
      total: 100,
      value2: "",
      form: {
        itemName: "",
        itemPrice: "",
        itemBrand: "",
        startTime: "",
        endTime: "",
        pageNo: 1,
        pageSize: 2
      },
      sum: null,
    };
  }
};
</script>